package cn.fintecher.pangolin.service.business.service;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.enums.State;
import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.ComplianceCheck;
import cn.fintecher.pangolin.entity.mysql.Resource;
import cn.fintecher.pangolin.entity.mysql.SimpleUser;
import cn.fintecher.pangolin.entity.mysql.User;
import cn.fintecher.pangolin.service.business.model.request.ComplianceCheckRequest;
import cn.fintecher.pangolin.service.business.repository.jparepository.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ComplianceCheckService {

    @Autowired
    private ComplianceCheckRepository complianceCheckRepository;

    @Autowired
    private ResourceRepository resourceRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private SimpleUserRepository simpleUserRepository;

    @Autowired
    private SimplePrincipalRepository simplePrincipalRepository;

    /**
     * @Author wangxiangdong
     * @Description 新增合规检查配置
     * @Date 2019/10/19 10:37
     **/
    @Transactional
    public void createComplianceCheck(ComplianceCheckRequest complianceCheckRequest, UserModel userModel) {
        ComplianceCheck complianceCheck;
        if(StringUtils.isNotBlank(complianceCheckRequest.getId())){
            complianceCheck = complianceCheckRepository.findById(complianceCheckRequest.getId()).orElseThrow(()->new BadRequestException(null,"createComplianceCheck","compliance.config.is.not.exists"));
            simpleUserRepository.deleteAll(complianceCheck.getTarget());
            simplePrincipalRepository.deleteAll(complianceCheck.getPrincipals());
        }else{
            complianceCheck = new ComplianceCheck();
        }
        complianceCheck.setConfigName(complianceCheckRequest.getConfigName());
        complianceCheck.setStatus(complianceCheckRequest.getStatus());
        complianceCheck.setOperateTime(ZWDateUtil.getNowDateTime());
        complianceCheck.setOperator(userModel.getRealName());
        complianceCheck.setPrincipals(complianceCheckRequest.getPrincipals());
        complianceCheck.setTarget(complianceCheckRequest.getUsers());
        if(Objects.nonNull(complianceCheckRequest.getResourceIds())){
            List<Resource> resources = resourceRepository.findAllById(complianceCheckRequest.getResourceIds());
            Set<Resource> resourceSet = new HashSet<>(resources);
            complianceCheck.setResources(resourceSet);
        }
        if(Objects.equals(complianceCheckRequest.getStatus(),State.ENABLE)){
            List<User> users = userRepository.findAllById(complianceCheckRequest.getUsers().stream().map(SimpleUser::getUserId).collect(Collectors.toList()));
                users.forEach(user->{
                    user.setIsCheck(BaseSelect.YES);
                });
            userRepository.saveAll(users);
        }
        complianceCheckRepository.save(complianceCheck);
    }

    /**
     * @Author wangxiangdong
     * @Description 删除
     * @Date 2019/10/19 11:02
     **/
    @Transactional
    public void deleteComplianceCheck(String id) {
       ComplianceCheck complianceCheck = complianceCheckRepository.findById(id).orElseThrow(()->new BadRequestException(null,"createComplianceCheck","compliance.config.is.not.exists"));
        Set<SimpleUser> target = complianceCheck.getTarget();
        List<User> userList = userRepository.findAllById(target.stream().map(SimpleUser::getUserId).collect(Collectors.toList()));
        userList.forEach(user -> {
            user.setIsCheck(BaseSelect.NO);
        });
        userRepository.saveAll(userList);
       complianceCheckRepository.delete(complianceCheck);
    }

    /**
     * @Author wangxiangdong
     * @Description 更改状态
     * @Date 2019/10/19 11:02
     **/
    @Transactional
    public void changeState(String id) {
        ComplianceCheck complianceCheck = complianceCheckRepository.findById(id).orElseThrow(()->new BadRequestException(null,"createComplianceCheck","compliance.config.is.not.exists"));
        BaseSelect isCheck = BaseSelect.NO;
        State status = State.DISABLE;
        if(Objects.equals(complianceCheck.getStatus(),State.DISABLE)){
            status = State.ENABLE;
            isCheck = BaseSelect.YES;
        }
        complianceCheck.setStatus(status);
        Set<SimpleUser> target = complianceCheck.getTarget();
        List<User> userList = userRepository.findAllById(target.stream().map(SimpleUser::getUserId).collect(Collectors.toList()));
        for(User user : userList){
            user.setIsCheck(isCheck);
        }
        userRepository.saveAll(userList);
        complianceCheckRepository.save(complianceCheck);
    }

}
